<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px"
      class="white-normal-box">
      <el-form-item label="用户名称" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名称" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="white-normal-box">
      <el-row :gutter="10" class="mb20">
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="pcmList" @selection-change="handleSelectionChange">
        <!-- <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="主键id" align="center" prop="id" /> -->
        <el-table-column label="用户名称" align="center" prop="userName" />
        <el-table-column label="pc端口机器数量" align="center" prop="pcNumber" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
            >详情</el-button>
            <el-button type="text" icon="el-icon-download" size="mini"
              @click="handleExport(scope.row)">导出</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    </div>

    <!-- 添加或修改金币等级对话框 -->
    <el-dialog title="机器详情" :visible.sync="open" width="700px" append-to-body>
      <el-table :data="infoList" max-height="500">
        <el-table-column label="订单号" align="center" prop="id" show-overflow-tooltip />
        <el-table-column label="商品名" align="center" prop="orderName" min-width="120" />
        <el-table-column label="价格" align="center" prop="orderPrice" />
        <el-table-column label="支付类型" align="center" prop="buyType">
          <template slot-scope="{ row }">
            <div>
              {{ ['线下支付', '微信支付', '余额支付', '支付宝支付'][row.buyType || 0] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="订单状态" align="center" prop="orderState">
          <template slot-scope="{ row }">
            <div>
              {{ ['待支付', '已支付'][row.orderState || 0] }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="用户名称" align="center" prop="userName" />
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPCM, getPCMInfo } from "@/api/system/pcMachine";
export default {
  name: "newUser",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 金币等级表格数据
      pcmList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: undefined,
        startTime: undefined,
        endTime: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      infoList: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询金币等级列表 */
    getList() {
      this.loading = true;
      listPCM(this.queryParams).then(response => {
        this.pcmList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        startTime: undefined,
        endTime: undefined,
        userId: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.infoList = []
      getPCMInfo({userId: row.userId}).then(res => {
        this.infoList = res.data
        this.open = true;
      })
    },
    /** 导出按钮操作 */
    handleExport(row) {
      this.download('system/order/pcExport', {
        userName: row.userName
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
